package com.itany.controller;


import com.github.pagehelper.PageInfo;
import com.itany.constant.ResponseConstant;
import com.itany.exception.RequestParamsException;
import com.itany.exception.SupplierExistsException;
import com.itany.pojo.Supplier;
import com.itany.service.SupplierService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    SupplierService supplierService;

    @RequestMapping("/findAllNoPages")
    @ResponseBody
    public List<Supplier> findAllNoPages() {
        return supplierService.findAllNoPages();
    }


    /**
     * 查找所有供应商
     *
     * @param page 页码数
     * @param rows 每页数据条数
     * @return 分页供应商信息
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer rows,
                                       String suppliernameKey) {
        Map<String, Object> map = new HashMap<>();
        PageInfo<Supplier> info = null;
        try {
            info = supplierService.findAll(page, rows,suppliernameKey);
        } catch (RequestParamsException e) {
            e.printStackTrace();
        }
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }


    /**
     * 添加供应商
     * @param suppliername 名称
     * @param linkman 联系人
     * @param phone 电话
     * @param info 公司信息
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public ActionResult addSupplier(String suppliername,String linkman,String phone,String info) {
        ActionResult result = new ActionResult();
        try {
            supplierService.addSupplier(suppliername,linkman,phone,info);
            result.setMsg("添加成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }


    /**
     * 修改指定供应商信息
     * @param id 供应商id
     * @param suppliername 名称
     * @param linkman 联系人
     * @param phone 电话
     * @param info 公司信息
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public ActionResult modifySupplier(String id, String suppliername,String linkman,String phone,String info) {
        ActionResult result = new ActionResult();
        try {
            supplierService.modifySupplier(id,suppliername,linkman,phone,info);
            result.setMsg("修改成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        }catch (SupplierExistsException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.RESPONSE_FAIL);
        }catch (Exception e) {
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }


    /**
     * 设置指定供应商状态
     * @param id 供应商id
     * @param status 该供应商待修改的状态
     * @return 响应数据
     */
    @RequestMapping("/status/{status}")
    @ResponseBody
    public ActionResult enable(String id, @PathVariable("status") Integer status){
        ActionResult result = new ActionResult();
        try {
            supplierService.modifyStatus(id,status);
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }
}
